﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Kooboo.HtmlFunc.Function;
using Kooboo.HtmlFunc.Providers;

namespace Kooboo.HtmlFunc.Analysis.Visitors
{
    public class PositionVisitor : StartEndVisitor
    {
        public PositionVisitor(VisitorContext context)
            : base(context, FuncType.Position)
        {
        }

        private IFuncTester _startCommentTester;
        protected override IFuncTester StartCommentTester
        {
            get
            {
                if (_startCommentTester == null)
                    _startCommentTester = new Testers.ParametricTester(this.Func.StartComment);
                return _startCommentTester;
            }
        }

        protected override object OnQualified(HtmlCommentNode startNode, HtmlCommentNode endNode)
        {
            // add
            var postion = Provider.Factory.GetProvider<IPosition>();
            postion.StartNode = startNode;
            postion.EndNode = endNode;
            postion.Name = this.GenerateName();
            this.Context.Result.Positions.Add(postion);

            // ret context mark
            return postion;
        }

        private string GenerateName()
        {
            var tester = (Testers.ParametricTester)this.StartCommentTester;

            var name = tester.GetParameter("name");

            // default name
            if (string.IsNullOrEmpty(name))
                name = string.Format("Position{0}", this.Context.Result.Positions.Count);

            return name;
        }

        protected override FuncType ExclusiveType
        {
            get
            {
                return this.FuncType
                    | FuncType.Label
                    | FuncType.HtmlBlockPart;
            }
        }
    }
}
